package com.prs.crm.service.stock.impl;

import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;

import com.prs.crm.context.Constrains;
import com.prs.crm.dao.stock.StoreFlitDao;
import com.prs.crm.domain.flow.WorkFlowProcess;
import com.prs.crm.domain.stock.Product;
import com.prs.crm.domain.stock.ProductStore;
import com.prs.crm.domain.store.StoreFlit;
import com.prs.crm.domain.store.StoreFlitDetail;
import com.prs.crm.domain.store.StoreRoom;
import com.prs.crm.service.impl.FlowServiceImpl;
import com.prs.crm.service.stock.ProductStoreService;
import com.prs.crm.service.stock.StoreFlitService;

public class StoreFlitServiceImpl extends
		FlowServiceImpl<StoreFlit, StoreFlitDao> implements StoreFlitService {

	@Autowired
	private ProductStoreService productStoreService;

	@Override
	public void saveAllStoreFlit(StoreFlit storeflit, Product[] products,
			Integer[] pronums, String[] names, Integer[] storeoutNums,
			String[] remarks, StoreRoom[] rooms) {
		for (int i = 0; i < pronums.length; i++) {
			ProductStore pspo = this.getProductStoreService().getStore(
					rooms[i], products[i], storeflit.getBatchNum());
			pspo.setFlitNum(pspo.getFlitNum()+storeoutNums[i]);
			StoreFlitDetail detail = new StoreFlitDetail();
			detail.setName(products[i].getName());
			detail.setProduct(products[i]);
			detail.setRemark(remarks[i]);
			detail.setStoreflitNum(storeoutNums[i]);
			storeflit.addDetail(detail);
			this.getProductStoreService().save(pspo);
		}
		super.save(storeflit);
	}

	@Override
	public void onProcessEnd(WorkFlowProcess process) {

		super.onProcessEnd(process);

		StoreFlit flit = this.getProcessEntity(process);
		Set<StoreFlitDetail> storeFlitDetails = flit.getDetails();
		if (process.getResult() == Constrains.WORKFLOWPROCESS_RESULT_PASS) {
			for (StoreFlitDetail storeFlitDetail : storeFlitDetails) {
				ProductStore pspo = this.getProductStoreService().getStore(
						flit.getFlitOutRoom(), storeFlitDetail.getProduct(),
						flit.getBatchNum());
				pspo.setProductNum(pspo.getProductNum()
						- storeFlitDetail.getStoreflitNum());
				pspo.setFlitNum(pspo.getFlitNum()-storeFlitDetail.getStoreflitNum());
				this.getProductStoreService().savePrStore(pspo);
				int size = checkRecord(storeFlitDetail.getProduct().getId(),
						flit.getFlitInRoom().getId(), flit.getBatchNum());
				if (size == 0) {
					saveNewProductStore(flit, storeFlitDetail.getProduct(),
							storeFlitDetail.getStoreflitNum());
				} else {

					updateProductStore(storeFlitDetail.getStoreflitNum(), flit
							.getFlitInRoom(), storeFlitDetail.getProduct(),
							flit.getBatchNum());
				}
			}
		} else {
			for (StoreFlitDetail storeFlitDetail : storeFlitDetails) {		
					ProductStore pspo = this.getProductStoreService().getStore(
							flit.getFlitOutRoom(), storeFlitDetail.getProduct(), flit.getBatchNum());
					pspo.setFlitNum(pspo.getFlitNum()-storeFlitDetail.getStoreflitNum());
					this.getProductStoreService().save(pspo);
			}
		}
	}

	public ProductStoreService getProductStoreService() {
		return productStoreService;
	}

	public void setProductStoreService(ProductStoreService productStoreService) {
		this.productStoreService = productStoreService;
	}

	public int checkRecord(int product, int room, String batchNum) {
		List<ProductStore> slist = this.getProductStoreService()
				.getProductStoreSize(product, room, batchNum);

		return slist.size();
	}

	public void saveNewProductStore(StoreFlit storein, Product product,
			int stocknum) {
		ProductStore ps = new ProductStore();
		ps.setRoom(storein.getFlitInRoom());
		ps.setProduct(product);
		ps.setProductNum(stocknum);
		ps.setBatchNum(storein.getBatchNum());
		ps.setStorecode(storein.getFlitInRoom().getRoomcode());
		this.getProductStoreService().save(ps);
	}

	public void updateProductStore(int stocknum, StoreRoom room,
			Product product, String batchNum) {
		ProductStore prst = this.getProductStoreService().getStore(room,
				product, batchNum);
		prst.setProductNum(prst.getProductNum() + stocknum);

		this.getProductStoreService().savePrStore(prst);
	}

	@Override
	public boolean valideSubmit(StoreFlit entity) {

		return true;
	}

}
